package com.yonyou.crm.bpub.initData.service;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.bpub.initData.entity.InitDatasBodyVO;
import com.yonyou.crm.bpub.initData.entity.InitDatasItemVO;
import com.yonyou.crm.bpub.initData.entity.InitDatasListItemVO;
import com.yonyou.crm.bpub.initData.entity.InitDatasVO;
import com.yonyou.crm.common.exception.CrmBusinessException;
import com.yonyou.crm.sprc.opportunity.entity.OpportunityBVO;
import com.yonyou.crm.sprc.opportunity.entity.OpportunityVO;
import com.yonyou.crm.sprc.opportunity.service.OpportunityBizServiceImpl;
import com.yonyou.crm.sys.user.entity.UserVO;

@Service
public class OppInitData extends BaseInitDatas {
	
	@Autowired
	OpportunityBizServiceImpl service;
	
	@Override
	public <T extends Serializable> T getDetail(long id) {
		return (T)service.getDetail(id);
	}

	@Override
	public ArrayList<InitDatasListItemVO> getHeader(InitDatasVO initData) {
		ArrayList<InitDatasListItemVO> headerList = new ArrayList<InitDatasListItemVO>();
		InitDatasListItemVO item;
		ArrayList<InitDatasItemVO> itemDataList;
		InitDatasItemVO itemData;
			
		item = new InitDatasListItemVO("id");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("0");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("name");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("customerId");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("saleStage");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("winProbability");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("expectSignTime");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("expectSignMoney");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("ownerUserId");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("relUsers");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("type");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("customerBudget");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		item = new InitDatasListItemVO("createdTime");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO(sdf.format(date));
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);
		
		item = new InitDatasListItemVO("description");
		itemDataList = new ArrayList<InitDatasItemVO>();
		itemData = new InitDatasItemVO("");
		itemDataList.add(itemData);
		item.setItemdatalist(itemDataList);
		headerList.add(item);

		return headerList;
	}

	@Override
	public ArrayList<InitDatasBodyVO> getBody(InitDatasVO initData) {
		ArrayList<InitDatasBodyVO> bodyList = new ArrayList<InitDatasBodyVO>();
		return bodyList;
	}

	@Override
	public ArrayList<InitDatasBodyVO> getBodyDefault() {

		ArrayList<InitDatasBodyVO> bodyList = new ArrayList<InitDatasBodyVO>();
		
		InitDatasBodyVO bodyVO = new InitDatasBodyVO();
		List<List<InitDatasListItemVO>> bodyDataList = new ArrayList<List<InitDatasListItemVO>>();
		ArrayList<InitDatasListItemVO> itemListVOs = new ArrayList<InitDatasListItemVO>();
		
		InitDatasListItemVO itemListVO;
		ArrayList<InitDatasItemVO> itemVOs;
		InitDatasItemVO itemVO;
		bodyVO.setBodymark("product1");
		
		itemListVO = new InitDatasListItemVO();
		itemListVO.setItemkey("productId");
		itemVOs = new ArrayList<InitDatasItemVO>();
		itemVO = new InitDatasItemVO();
		itemVO.setDataid("");
		itemVO.setDatavalue("");
		itemVOs.add(itemVO);
		itemListVO.setItemdatalist(itemVOs);
		itemListVOs.add(itemListVO);

		itemListVO = new InitDatasListItemVO();
		itemListVO.setItemkey("productTypeId");
		itemVOs = new ArrayList<InitDatasItemVO>();
		itemVO = new InitDatasItemVO();
		itemVO.setDataid("");
		itemVO.setDatavalue("");
		itemVOs.add(itemVO);
		itemListVO.setItemdatalist(itemVOs);
		itemListVOs.add(itemListVO);

		itemListVO = new InitDatasListItemVO();
		itemListVO.setItemkey("brandId");
		itemVOs = new ArrayList<InitDatasItemVO>();
		itemVO = new InitDatasItemVO();
		itemVO.setDataid("");
		itemVO.setDatavalue("");
		itemVOs.add(itemVO);
		itemListVO.setItemdatalist(itemVOs);
		itemListVOs.add(itemListVO);

		itemListVO = new InitDatasListItemVO();
		itemListVO.setItemkey("price");
		itemVOs = new ArrayList<InitDatasItemVO>();
		itemVO = new InitDatasItemVO();
		itemVO.setDataid("");
		itemVO.setDatavalue("");
		itemVOs.add(itemVO);
		itemListVO.setItemdatalist(itemVOs);
		itemListVOs.add(itemListVO);

		itemListVO = new InitDatasListItemVO();
		itemListVO.setItemkey("number");
		itemVOs = new ArrayList<InitDatasItemVO>();
		itemVO = new InitDatasItemVO();
		itemVO.setDataid("");
		itemVO.setDatavalue("");
		itemVOs.add(itemVO);
		itemListVO.setItemdatalist(itemVOs);
		itemListVOs.add(itemListVO);
		
		bodyDataList.add(itemListVOs);
		bodyVO.setBodydatalist(bodyDataList);
		bodyList.add(bodyVO);
		
		return bodyList;
	}
	
	/**
	 * 获取模板配置
	 */
	@Override
	public <T extends Serializable> InitDatasVO getLayout(T obj){
		OpportunityVO vo = (OpportunityVO) obj;
		//Long biztypeId = vo.getBiztypeId();
		long biztypeId = 11;
		return layoutService.getInitDatasForMobile(biztypeId);
	}

	@Override
	public <T extends Serializable> ArrayList<InitDatasListItemVO> getHeader(T obj, InitDatasVO initData) {
		OpportunityVO vo = (OpportunityVO) obj;
		if (vo == null) {
			throw new CrmBusinessException("商机不存在");
		}
		
		ArrayList<InitDatasListItemVO> headerList = new ArrayList<InitDatasListItemVO>();
		String[] itemkeyList = {"id","name","customerId","saleStage","winProbability","expectSignTime","expectSignMoney","ownerUserId","relUsers","type","customerBudget","createdTime","description"};
		for (int i = 0; i< itemkeyList.length; i++) {
			InitDatasListItemVO headerItem = new InitDatasListItemVO();
			headerItem.setItemkey(itemkeyList[i]);
			ArrayList<InitDatasItemVO> itemdatalist = new ArrayList<>();
			InitDatasItemVO itemdata = new InitDatasItemVO();
			String dataid = "";
			String datavalue = "";
			if ( itemkeyList[i].equals("customerId")) {
				dataid =(vo.getCustomerId() == null) ? "" : String.valueOf(vo.getCustomerId());
				if (vo.getCustomerInfo() != null) {
					datavalue = (vo.getCustomerInfo().getName() == null) ? "" : vo.getCustomerInfo().getName();
				}
			} else if (itemkeyList[i].equals("saleStage")) {
				dataid =(vo.getSaleStage() == null) ? "" : String.valueOf(vo.getSaleStage());
				datavalue = (vo.getSaleStageName() == null) ? "" : vo.getSaleStageName();
			} else if (itemkeyList[i].equals("ownerUserId")) {
				dataid =(vo.getOwnerUserId() == null) ? "" : String.valueOf(vo.getOwnerUserId());
				datavalue = (vo.getOwnerUserName() == null) ? "" : vo.getOwnerUserName();
			} else if (itemkeyList[i].equals("relUsers")) {
				List<UserVO> relUserList = vo.getRelUserList();
				for (UserVO user:relUserList) {
					if (dataid.isEmpty()) {
						dataid = user.getId().toString();
						datavalue = user.getName();
					} else {
						dataid = dataid + "," + user.getId().toString();
						datavalue = datavalue + "," + user.getName();
					}
				}
			} else if (itemkeyList[i].equals("type")) {
				dataid =(vo.getType() == null) ? "" : String.valueOf(vo.getType());
				datavalue = (vo.getTypeName() == null) ? "" : vo.getTypeName();
			} else {
				char[] cs = itemkeyList[i].toCharArray();
				cs[0] = Character.toUpperCase(cs[0]);
				String funcName = String.valueOf(cs);
				try {
					Object data = vo.getClass().getMethod("get"+funcName, new Class[]{}).invoke(vo, new Object[]{});
					dataid= (data == null) ? "" : data.toString();
					datavalue = dataid;
				} catch (IllegalAccessException
						| IllegalArgumentException
						| InvocationTargetException
						| NoSuchMethodException | SecurityException e) {
					throw new CrmBusinessException("动态调用opp方法错误！");
				}
			}
			itemdata.setDataid(dataid);
			itemdata.setDatavalue(datavalue);
			itemdatalist.add(itemdata);
			headerItem.setItemdatalist(itemdatalist);
			headerList.add(headerItem);
			
		}
		return headerList;
	}

	@Override
	public <T extends Serializable> ArrayList<InitDatasBodyVO> getBody(T obj, InitDatasVO initData) {
		OpportunityVO vo = (OpportunityVO)obj;
		if (vo == null) {
			throw new CrmBusinessException("商机不存在");
		}
		List<OpportunityBVO> listBvos = vo.getChildList();
		ArrayList<InitDatasBodyVO> bodyList = new ArrayList<InitDatasBodyVO>();
		if (listBvos == null || listBvos.size() == 0) {
			return bodyList;
		}
		
		InitDatasBodyVO bodyVO = new InitDatasBodyVO();
		bodyVO.setBodymark("product1");
		List<List<InitDatasListItemVO>> bodyDataList = new ArrayList<List<InitDatasListItemVO>>();
		
		String[] itemkeyList = {"id","productId","productTypeId","brandId","price","number"};
		for (int i = 0; i < listBvos.size(); i++) {
			OpportunityBVO bvo = listBvos.get(i);
		    List<InitDatasListItemVO> listItemVOs = new ArrayList<InitDatasListItemVO>();
			for (int j = 0; j < itemkeyList.length; j++) {
				InitDatasListItemVO listItemVO = new InitDatasListItemVO();
				listItemVO.setItemkey(itemkeyList[j]);
				ArrayList<InitDatasItemVO> itemdatalist = new ArrayList<>();
				InitDatasItemVO itemdata = new InitDatasItemVO();
				String dataid = "";
				String datavalue = "";
				if (itemkeyList[j].equals("productId")) {
					dataid =(bvo.getProductId() == null) ? "" : String.valueOf(bvo.getProductId());
					datavalue = (bvo.getProductInfo() == null || bvo.getProductInfo().getName() == null) ? "" : bvo.getProductInfo().getName();
				} else if ( itemkeyList[j].equals("productTypeId")) {
					dataid =(bvo.getProductTypeId() == null) ? "" : String.valueOf(bvo.getProductTypeId());
					datavalue = (bvo.getProductTypeName() == null) ? "" : bvo.getProductTypeName();
				} else if (itemkeyList[j].equals("brandId")) {
					dataid =(bvo.getBrandId() == null) ? "" : String.valueOf(bvo.getBrandId());
					datavalue = (bvo.getBrandName() == null) ? "" : bvo.getBrandName();
				} else {
					char[] cs = itemkeyList[j].toCharArray();
					cs[0] = Character.toUpperCase(cs[0]);
					String funcName = String.valueOf(cs);
					try {
						Object data = bvo.getClass().getMethod("get"+funcName, new Class[]{}).invoke(bvo, new Object[]{});
						//dataid = (data == null) ? "" : data.toString();
						datavalue = (data == null) ? "" : data.toString();
					} catch (IllegalAccessException
							| IllegalArgumentException
							| InvocationTargetException
							| NoSuchMethodException | SecurityException e) {
						throw new CrmBusinessException("动态调用opp明细方法错误！");
					}
				}
				itemdata.setDataid(dataid);
				itemdata.setDatavalue(datavalue);
				itemdatalist.add(itemdata);
				listItemVO.setItemdatalist(itemdatalist);
				listItemVOs.add(listItemVO);
			}
			bodyDataList.add(listItemVOs);
		}
		bodyVO.setBodydatalist(bodyDataList);
		bodyList.add(bodyVO);
		
		return bodyList;
	}

}
